package com.example.test.conview.view.core;

import android.os.Handler;
import android.os.Message;

import com.puty.conputyutils.LogUtils;
import com.puty.conputyview.bean.ModelBase;
import com.puty.conputyview.common.PutyConst;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 标签实体类
 */
public class Label implements Cloneable, Serializable {

    @Override
    public Label clone() throws CloneNotSupportedException {
        Label lb = new Label(this.LabelName, this.Width, this.Height);
        lb.backGroundImageUrl = this.backGroundImageUrl;
        lb.preview_image = this.preview_image;
        lb.updateTime = this.updateTime;
        lb.printInfo = this.printInfo.clone();
        lb.colNameStrings = this.colNameStrings;
        lb.DataSourcePath = this.DataSourcePath;
        lb.leftMargin = this.leftMargin;
        lb.LabelId = this.LabelId ;
        lb.scale = this.scale;
        lb.topMargin = this.topMargin;
        lb.isLock = this.isLock;
        lb.rfidisLock = this.rfidisLock;
        lb.isNotSave = this.isNotSave;
        lb.Width = this.Width;
        lb.Height = this.Height;
        lb.fixedLength = this.fixedLength;
        lb.printInfo.blankArea = this.printInfo.blankArea;
        lb.alignment = this.alignment;
        lb.offsetX = this.offsetX;
        lb.offsetY = this.offsetY;
        lb.printInfo.printCopies = this.printInfo.printCopies;
        lb.isMunSelect = this.isMunSelect;
        lb.mirrorLabelType = this.mirrorLabelType;
        lb.labelHeight = this.labelHeight;
        //线缆镜像值拷贝
        lb.isCableLabelInt=this.isCableLabelInt;
        lb.tailDirectionInt=this.tailDirectionInt;
        lb.tailLengthDouble=this.tailLengthDouble;

        lb.dpih = this.dpih;
        lb.dpihscale = this.dpihscale;
        lb.share_againscale = this.share_againscale;


        for (BaseElement el : this.Elements){
            el.scale = this.scale;
            BaseElement newel = null;
            newel = ((Element)el).clone(this);
            lb.Elements.add(newel);
        }
        return lb;
    }
    public int rfidMode = 0;//是否是RFID标签  0 普通标签  1高频标签  2代表超高频标签EPC区   3代表超高频标签User区
    public int rfidDataMode = 0;//0手动输入   1 导入Excel表格
    public String rfidContent = "";//RFID内容
    public long rfidDataStep = 0;//递变量
    public int rfidDataSourceColIndex = -1;//列号
    public List<List<String>> excelDataSource = new ArrayList<>();//打印Excel数据源

    /**
     *  标签  id
     */
    public String LabelId = "new_puty" + System.currentTimeMillis();
    /**
     * 标签名
     */
    public String LabelName = "";
    /**
     * 更新时间
     */
    public String updateTime; // 更新时间
    /**
     * 预览图
     */
    public String preview_image; // 预览图
    /**
     * 标签是否锁定
     */
    public int isLock = 0;
    /**
     * rfid开关
     */
    public int rfidisLock = 0;
    /**
     * 多选
     */
    public int isMunSelect = 0;


    /**
     * // 缩放比率
     */
    public float scale = 1.0f;
    public float dpih= 0;
    public float dpihscale= 0;
    public float share_againscale=1;
    public float Width = 70;// mm
    public float Height = 50;// mm
    public float labelHeight; // 实际标签高度
    public double offsetX = 0.0;// mm
    public double offsetY = 0.0;// mm
    public int fixedLength = 0; // 是否固定长度，0自动；1固定。用于PT-210E
    public int alignment = 0; // 对齐方式，0居左； 1居中； 2居右

    /**
     * // 左侧间距
     */
    public float leftMargin = 0.00f;
    /**
     * // 顶部间距
     */
    public float topMargin = 0.00f;
    /**
     * 背景图地址或base64格式图片
     */
    public String backGroundImageUrl = "";
    /**
     * // 打印参数
     */
    public PrintInfo printInfo = new PrintInfo();

    /**
     * // 没有保存
     */
    public boolean isNotSave = false;
    /**
     * 数据库文件地址(xls)
     */
    public String DataSourcePath = "";

    /**
     * // 所有列名称
     */
    public String[] colNameStrings;

    /**
     * 所有元素列表
     */
    public int ElementCount = 0;
    /**
     * 缩放比例
     */
    public float scalingRatio = 1;
    /**
     * 镜像标签类型 0无   1上下镜像   2左右镜像  3元素镜像
     */
    public int mirrorLabelType;


    public List<BaseElement> Elements = new CopyOnWriteArrayList<>();

    public Handler ElementHandler;
    Lock lock = new ReentrantLock();

    /**
     * 是否有尾巴
     */
    public int isCableLabelInt = 0;
    /**
     * 尾巴方向
     */
    public int tailDirectionInt = 1;
    /**
     * 尾巴长度
     */
    public double tailLengthDouble = 0;

    /**
     * 添加元素
     *
     * @param key
     * @param el
     */
    public void AddElement(String key, BaseElement el) {
        lock.lock();
        this.Elements.add(el);
        lock.unlock();
        this.ElementCount = Elements.size();
        if (ElementHandler == null)
            return;
        ElementHandler.dispatchMessage(new Message());
    }

    /**
     * 获取元素
     */
    public BaseElement getElement(String id) {
        for (BaseElement el : this.Elements) {
            if (el.entityId.equals(id)) return el;
        }
        return null;
    }



    public Label(String name, float width, float height) {
        this.LabelName = name;
        this.Width = width;
        this.Height = height;
    }



    /**
     * 计算边线距离
     *
     * @return
     */
    private static final String TAG2 = "xflag";
    public float getSideLineDistance(boolean xflag) {
        int machineType = 1;
        float sideLineDistanceAbout = 0;
        if (printInfo != null && printInfo.arrayModel != null && printInfo.arrayModel.length > 0) {
            machineType = printInfo.arrayModel[0];
        }
        ModelBase modelBase = null;
//                StaticVariable.getModelBaseBySeriesId();
        if(PutyConst.netFlag){
            //加载网络机型数据
            if(xflag){
                if (Width > modelBase.getPrintWidth()) {//左右边线距离
                    sideLineDistanceAbout = modelBase.getPrintHeadX();//2
                } else {
                    sideLineDistanceAbout = 0;
                }
                LogUtils.i(TAG2,"sideLineDistanceAboutx:"+sideLineDistanceAbout);
            }else{
                    sideLineDistanceAbout = modelBase.getPrintHeadY();
                LogUtils.i(TAG2,"sideLineDistanceAbouty:"+sideLineDistanceAbout);
            }

        }else {
            // 本地机型数据
            if (modelBase.getName().startsWith("50DC") || modelBase.getName().startsWith("52DC") || modelBase.getName().startsWith("55DC")) {//50 \ 旧51 \55
                if (Width > 48) {//左右边线距离
                    sideLineDistanceAbout = 2;
                } else {
                    sideLineDistanceAbout = 0;
                }
            } else if (modelBase.getName().startsWith("56DC") || modelBase.getName().startsWith("51DC")) {//新C51DC-->C56DC-->PT-51DC
                if (Width > 48) {//左右边线距
                    sideLineDistanceAbout = 2;
                } else {
                    sideLineDistanceAbout = 0;
                }
            } else if (modelBase.getName().startsWith("65DC") || modelBase.getName().startsWith("66DC") || modelBase.getName().startsWith("67DC") || modelBase.getName().startsWith("69DC") || modelBase.getName().startsWith("60DC") || modelBase.getName().startsWith("68DC") || modelBase.getName().startsWith("SP60") || modelBase.getName().startsWith("63DC") || modelBase.getName().startsWith("PUTY-HD61-H")) {//66 \ 60 \ 68
                if (Width > 48) {//左右边线距离
                    sideLineDistanceAbout = 2;
                } else {
                    sideLineDistanceAbout = 1;
                }
            } else if (modelBase.getName().startsWith("64DC")) {//C-64DC
                if (Width > 48) {//左右边线距离
                    sideLineDistanceAbout = 2;
                } else {
                    sideLineDistanceAbout = 1;
                }
            } else if (modelBase.getName().startsWith("82DC")) {//82
                if (Width > 48) {//左右边线距离
                    sideLineDistanceAbout = 3;
                } else {
                    sideLineDistanceAbout = 2;
                }
            } else if (modelBase.getName().startsWith("KR80")) {//82
                if (Width > 48) {//左右边线距离
                    sideLineDistanceAbout = 3;
                } else {
                    sideLineDistanceAbout = 2;
                }
            } else if (modelBase.getName().startsWith("112DC")) {//112
                if (Width > 48) {//左右边线距离
                    sideLineDistanceAbout = 3;
                } else {
                    sideLineDistanceAbout = 2;
                }
            } else if (modelBase.getName().startsWith("210E") || modelBase.getName().startsWith("Q1")) {//200E
                sideLineDistanceAbout = 1;
            }
        }
        return sideLineDistanceAbout;
    }


}
